<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function generateGrid(objects) {
  // 创建一个 5 行 4 列的二维数组，初始值为 0
  const grid = Array.from({ length: 5 }, () => Array(4).fill(0));

  objects.forEach((obj) => {
    const x = Math.round(obj.x)
    const y = Math.round(obj.y)
    const width = Math.round(obj.width)
    const height = Math.round(obj.height)
    const startX = (x - 100) / 100;
    const startY = (y- 100) / 100;
    const endX = startX + width / 100;
    const endY = startY + height / 100;
    for (let i = startY; i < endY; i++) {
      for (let j = startX; j < endX; j++) {
        grid[i][j] = 1;
      }
    }
  });

  return grid;
}

// 示例数据
const objects = [
    {
        "x": 100,
        "y": 100,
        "width": 200,
        "height": 200
    },
    {
        "x": 400,
        "y": 200,
        "width": 100,
        "height": 200
    },
    {
        "x": 100,
        "y": 300,
        "width": 200,
        "height": 100
    },
    {
        "x": 300,
        "y": 99.9999999999999,
        "width": 200.000000000002,
        "height": 100.0000000000001
    },
    {
        "x": 300,
        "y": 200,
        "width": 100,
        "height": 200
    },
    {
        "x": 400,
        "y": 400,
        "width": 100,
        "height": 200
    },
    {
        "x": 300,
        "y": 400,
        "width": 100,
        "height": 100
    },
    {
        "x": 100,
        "y": 500,
        "width": 100,
        "height": 100
    },
    {
        "x": 100,
        "y": 400,
        "width": 100,
        "height": 100
    },
    {
        "x": 300,
        "y": 500,
        "width": 100,
        "height": 100
    }
];

// 生成二维数组
const grid = generateGrid(objects);
console.log(grid);

    </script>
</body>
</html>